#include <iostream>
int match(char* regexp, char * text);
int matchhere(char* regexp, char* text);
int matchstar(char c, char* regexp, char* text);

#define SHOW(sign) std::cout<<sign<<"\tregexp:"<<regexp<<"\t"<<"text:"<<text<<std::endl

int match(char* regexp, char * text){
    SHOW("match");
    if(regexp[0] == '^')
        return matchhere(regexp+1, text);

    do{
        if(matchhere(regexp,text))
            return 1;
        std::cout<<"========================"<<std::endl;
    }while(*text++);

    return 0;
}

int matchhere(char* regexp, char* text){
    SHOW("matchhere");

    if(!*regexp)
        return 1;

    if(regexp[0] == '$' && ! *text)
        return 1;

    if(regexp[1] == '*')
        return matchstar(regexp[0],regexp+2,text);

    if(regexp[0] == '.' && !*text)
        return matchhere(regexp+1,text+1);

    if(regexp[0] == text[0])
        return matchhere(regexp+1,text+1);

    return 0;
}

int matchstar(char c, char* regexp, char* text){
    SHOW("matchstar");
    do{
          if( matchhere(regexp,text) )
            return 1;
    }while(*text && (*text++ == c|| c == '.'));
    return 0;
}


int main()
{
	std::cout << match("w*o","Hello world!") << std::endl;
	return 0;
}
